import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [{
        path: '/login',
        component: () =>
            import ('@/views/Login'),
        hidden: true
    },
    {
        path: '/404',
        component: () =>
            import ('@/views/404'),
        hidden: true
    },
    {
        path: '/',
        name: 'Home',
        component: Layout,
        redirect: '/dashboard',
        children: [{
            path: '/dashboard',
            name: 'dashboard',
            component: () =>
                import ('@/views/dashboard.vue'),
            meta: { title: '首页', icon: 'dashboard' }
        }]
    },
    {
        path: '/backup',
        name: 'backup',
        component: Layout,
        meta: { title: '备份', icon: 'wenjian' },
        children: [{
            path: "sqltasklist",
            name: "sqltasklist",
            meta: { title: '数据库任务', icon: 'database' },
            component: () =>
                import ('@/views/sqltasklist')
        }, {
            path: "newTask",
            name: "newTask",
            hidden: true,
            meta: { title: '新建数据库任务', icon: 'database' },
            component: () =>
                import ('@/views/newTask/index')
        }, {
            path: 'addsqltask',
            name: 'addsqltask',
            hidden: true,
            component: () =>
                import ('@/views/addsqltask.vue'),
            meta: { title: '数据库任务', icon: 'database' }
        }, {
            path: 'addfiletask',
            name: 'addfiletask',
            hidden: true,
            component: () =>
                import ('@/views/addfiletask.vue'),
            meta: { title: '文件任务' }

        }, {
            path: 'filetasklist',
            name: 'filetasklist',
            component: () =>
                import ('@/views/filetasklist'),
            meta: { title: '文件任务', icon: 'wenjian' },
        }, {
            path: 'tasklog',
            name: 'tasklog',
            component: () =>
                import ('@/views/tasklog.vue'),
            meta: { title: '本地备份日志', icon: 'log' }
        }]
    }, {
        path: "/remotessh",
        name: "remotessh",
        component: Layout,
        redirect: "/remotessh/sshtask",
        meta: { title: '远程备份', icon: 'database' },
        children: [{
                path: 'sshtask',
                name: 'sshtask',
                component: () =>
                    import ('@/views/sshtask.vue'),
                meta: { title: '远程备份', icon: 'database' },
            },
            {
                hidden: true,
                path: 'addsshtask',
                name: '添加任务',
                component: () =>
                    import ('@/views/addsshtask'),
                meta: { title: '任务' }
            }
        ]

    },
    {
        path: '/ftp',
        name: 'ftplist',
        component: Layout,
        meta: { title: '异地存储', icon: 'ftp' },
        children: [{
                path: '',
                component: () =>
                    import ('@/views/ftplist.vue'),
                meta: { title: '存储配置', icon: 'ftp' }
            },
            {
                hidden: true,
                path: 'addftp',
                name: '添加异地存储',
                component: () =>
                    import ('@/views/addftp'),
                meta: { title: '异地存储' }
            }, {
                path: 'ftplog',
                name: 'ftplog',
                component: () =>
                    import ('@/views/ftplog.vue'),
                meta: { title: '异地存储日志', icon: 'log' }
            },
        ]
    },
    {
        path: '/hosts',
        name: 'hosts',
        component: Layout,
        children: [{
                path: '',
                component: () =>
                    import ('@/views/hosts.vue'),
                meta: { title: '从机管理', icon: 'yserver' }
            },
            {
                hidden: true,
                path: 'addhost',
                name: '从机',
                component: () =>
                    import ('@/views/addhost'),
                meta: { title: '从机' }
            }
        ]
    },
    {
        path: '/yserver',
        name: 'yserver',
        component: Layout,
        meta: { title: 'YServer服务', icon: 'database' },
        children: [{
            path: '',
            component: () =>
                import ('@/views/yserver.vue'),
            meta: { title: '服务配置', icon: 'database' }
        }, {
            path: 'localstronge',
            name: 'localstronge',
            component: () =>
                import ('@/views/localstronge.vue'),
            meta: { title: 'Yserver日志', icon: 'log' }
        }]

    },
    {
        path: "/settings",
        name: "settings",
        component: Layout,
        redirect: "/settings/changepwd",
        meta: { title: '设置', icon: 'settings' },
        children: [{
                path: 'changepwd',
                name: 'changepwd',
                component: () =>
                    import ('@/views/changepwd.vue'),
                meta: { title: '修改密码', icon: 'password' }
            },
            {
                path: 'mail',
                name: 'mail',
                component: () =>
                    import ('@/views/mail.vue'),
                meta: { title: '邮件通知', icon: 'mail' }
            },
            {
                path: 'user',
                name: 'user',
                component: () =>
                    import ('@/views/user.vue'),
                meta: { title: '登录管理', icon: 'user' }
            },
            {
                path: 'usermsg',
                hidden: true,
                component: () =>
                    import ('@/views/msguser/index.vue'),
                meta: { title: '用户管理' }
            }
        ]

    },

    // 404 page must be placed at the end !!!
    { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
    const newRouter = createRouter()
    router.matcher = newRouter.matcher // reset router
}

export default router